package com.sip.stream.activity.preview.model;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.icu.util.Calendar;
import android.os.Build;

import com.sip.stream.R;
import com.sip.stream.media.ZFYMediaManager;
import com.sip.stream.media.ZFYMediaStatus;
import com.sip.stream.utils.zfy.PreviewDetectedBean;
import com.sip.stream.utils.zfy.ZFYUserInfo;

import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.PublishSubject;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PreviewViewModel {
    private WeakReference<Context> contextWeakReference;
    //时间
    public BehaviorSubject<String> nowTimeString = BehaviorSubject.create();
    PublishSubject<String> toast = PublishSubject.create();
    //存储
    private BehaviorSubject<String> tvStorage = BehaviorSubject.create();
    //存储颜色
    private BehaviorSubject<Integer> tvStorageColor = BehaviorSubject.create();
    //PPI
    BehaviorSubject<String> ppiStr = BehaviorSubject.create();
    //GPS
    public BehaviorSubject<String> gpsStr = BehaviorSubject.create();
    //屏幕打开
    BehaviorSubject<Boolean> keepScreenOn = BehaviorSubject.create();
    PublishSubject<PreviewDetectedBean> detectedBeanBehaviorSubject = PublishSubject.create();
    public BehaviorSubject<PreviewStaticDataViewModel> previewStaticDataSubject = BehaviorSubject.create();
    private PreviewStaticDataViewModel previewStatic = new PreviewStaticDataViewModel();
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public void showToast(String message) {
        this.toast.onNext(message);
    }

    public Observable<String> toastObservable() {
        return this.toast;
    }

    public PreviewViewModel(Context context) {
        this.contextWeakReference = new WeakReference<>(context);
    }

    private Context getContext() {
        return this.contextWeakReference.get();
    }

    private String getString(int resId) {
        return getContext().getResources().getString(resId);
    }

    private Drawable getDrawable(int resId) {
        return getContext().getResources().getDrawable(resId);
    }

    private int getColor(int resId) {
        return getContext().getResources().getColor(resId);
    }

    public Observable<String> getNowTime() {
        return this.nowTimeString;
    }

    public Observable<String> getToast() {
        return this.toast;
    }

    public Observable<String> getTvStorage() {
        return this.tvStorage;
    }

    public void setTvStorage(String size) {
        if (size == null) {
            size = "";
        }
        if (this.tvStorage.getValue() == null || !size.equalsIgnoreCase(this.tvStorage.getValue())) {
            this.tvStorage.onNext(size);
        }
    }

    public Observable<Integer> getTvStorageColor() {
        return this.tvStorageColor;
    }

    public void setTvStorageColor(int color) {
        this.tvStorageColor.onNext(Integer.valueOf(color));
    }

    public Observable<String> getPpiStr() {
        return this.ppiStr;
    }

    public void setPpiStr(String ppi) {
        this.ppiStr.onNext(ppi);
    }

    public void setZoomPersent(Float zoom) {
        this.previewStatic.zoomProgress = (int) (zoom.longValue() * 100);
        this.previewStaticDataSubject.onNext(this.previewStatic);
    }

    public Observable<String> getGpsStr() {
        return this.gpsStr;
    }

    public void setGPSStr(String gps) {
        this.gpsStr.onNext(gps);
    }

    public Observable<Boolean> getKeepScreenOnN() {
        return this.keepScreenOn;
    }

    public void setKEEP_SCREEN_ON(boolean screenOn) {
        this.keepScreenOn.onNext(Boolean.valueOf(screenOn));
    }

    public PublishSubject<PreviewDetectedBean> getDetectedBeanBehaviorSubject() {
        return this.detectedBeanBehaviorSubject;
    }

    public void refreshStaticData() {
        Date date = new Date();
        this.previewStatic.date = this.dateFormat.format(Long.valueOf(date.getTime())) + " " + getDayOfWeek();
        ZFYUserInfo currentUserInfo = ZFYUserInfo.defaultUserInfo();
        this.previewStatic.userCode = currentUserInfo.getUserCode();
        this.previewStatic.userName = currentUserInfo.getUserName();
        this.previewStatic.deviceCode = currentUserInfo.getVirtualSNCode();
        this.previewStatic.platformStatusString = getString(R.string.platform_status_online);
        this.previewStatic.platformStatusColor = -1;
        this.previewStaticDataSubject.onNext(this.previewStatic);
    }

    private String getDayOfWeek() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        {
            switch (Calendar.getInstance().get(7) - 1) {
                case 1:
                    return getString(R.string.preview_monday);
                case 2:
                    return getString(R.string.preview_tuesday);
                case 3:
                    return getString(R.string.preview_wednesday);
                case 4:
                    return getString(R.string.preview_thursday);
                case 5:
                    return getString(R.string.preview_friday);
                case 6:
                    return getString(R.string.preview_saturday);
                default:
                    return getString(R.string.preview_sunday);
            }
        }
        return  "";
    }

    public String getStatusString() {
        return this.previewStatic.statusString;
    }

    public boolean refreshRecordingAction() {
        ZFYMediaStatus status = ZFYMediaManager.getInstance().getStatus();
        this.previewStatic.showDate = false;
        if (status.isDelayRecording()) {
            this.previewStatic.statusDrawable = getDrawable(R.drawable.ic_vector_delayvideo);
            this.previewStatic.statusString = getString(R.string.preview_post_recording_short);
            this.previewStatic.statusColor = Color.parseColor("#D26913");
        } else if (status.isVideoStart()) {
            this.previewStatic.statusDrawable = getDrawable(R.drawable.ic_vector_videoing);
            this.previewStatic.statusString = "";
            this.previewStatic.statusColor = getColor(R.color.red_zfy);
        } else if (status.isAudioStart()) {
            this.previewStatic.statusDrawable = getDrawable(R.drawable.ic_vector_audioing);
            this.previewStatic.statusString = "";
            this.previewStatic.statusColor = getColor(R.color.red_zfy);
        } else if (status.isPreRecording()) {
            this.previewStatic.statusDrawable = getDrawable(R.drawable.ic_vector_prevideo);
            this.previewStatic.statusString = getString(R.string.preview_pre_recording_short);
            this.previewStatic.statusColor = getColor(R.color.green_zfy);
        } else if (status.isRemoteVideoAndAudio()) {
            this.previewStatic.statusDrawable = getDrawable(R.drawable.ic_vector_remote);
            this.previewStatic.statusString = "";
            this.previewStatic.statusColor = getColor(R.color.red_zfy);
        } else {
            this.previewStatic.statusString = "";
            this.previewStatic.statusColor = getColor(R.color.white);
            this.previewStatic.showDate = true;
            this.previewStatic.statusDrawable = null;
        }
        this.previewStaticDataSubject.onNext(this.previewStatic);
        return true;
    }
}
